#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <dirent.h>
#include <time.h>

#include "bash.h"
#include "pack.h"
#include "mypthread.h"
#include "file.h"

value *read_pzfile()
{
	value *head;
	head = (value *)malloc(sizeof(value));
	FILE *fp;
	fp = fopen ("pz1.txt","r");

	while(!feof(fp))
	{
		fscanf(fp,"%s",head->name);
		fscanf(fp,"%s",head->gxqm);
		fscanf(fp,"%s",head->bm);
		fscanf(fp,"%s",head->dh);
		fscanf(fp,"%s",head->yx);
		fscanf(fp,"%s",head->IP);
		fscanf(fp,"%s",head->Mac);
	}
	if(fp) fclose(fp);
	head->next = NULL;

	return head;
}

void cpy(value *p,pack *pack_head)
{
	pack_head->size = 0;
	strcpy(pack_head->name,p->name+5);
	strcpy(pack_head->bm,p->bm+3);
	strcpy(pack_head->Mac,p->Mac+4);
	strcpy(pack_head->dh,p->dh+3);
	strcpy(pack_head->yx,p->yx+3);
	strcpy(pack_head->IP,p->IP+3);
	strcpy(pack_head->gxqm,p->gxqm+5);
	pack_head->next = NULL;
}

int set_value(char *key,char *new_value,value *p)
{
	FILE *fp;
	fp = fopen ("pz1.txt","w");

	if(!strcmp(key,"name"))			
	{				
		strcpy(p->name,"name=");
		strcat(p->name,new_value);			
	}
	else if(!strcmp(key,"gxqm"))
	{	
		strcpy(p->gxqm,"gxqm=");
		strcat(p->gxqm,new_value);
	}
	else if(!strcmp(key,"bm"))
	{	
		strcpy(p->bm,"bm=");
		strcat(p->bm,new_value);
	}
	else if(!strcmp(key,"dh"))
	{	
		strcpy(p->dh,"dh=");
		strcat(p->dh,new_value);
	}
	else if(!strcmp(key,"yx"))
	{	
		strcpy(p->yx,"yx=");
		strcat(p->yx,new_value);
	}
	else if(!strcmp(key,"IP"))
	{	
		strcpy(p->IP,"IP=");
		strcat(p->IP,new_value);
	}
	else if(!strcmp(key,"Mac"))
	{	
		strcpy(p->Mac,"Mac=");
		strcat(p->Mac,new_value);
	}

	fprintf(fp,"%s\n",p->name);
	fprintf(fp,"%s\n",p->gxqm);
	fprintf(fp,"%s\n",p->bm);
	fprintf(fp,"%s\n",p->dh);
	fprintf(fp,"%s\n",p->yx);
	fprintf(fp,"%s\n",p->IP);
	fprintf(fp,"%s\n",p->Mac);

	if(fp) fclose(fp);
	return 0;
}

int get_value(char *key,value *p)
{
	if(!strcmp(key,"name"))	
		printf("\033[0;32;24mlinchen#\033[0m%s\n",p->name);
	else if(!strcmp(key,"gxqm"))	
		printf("\033[0;32;24mlinchen#\033[0m%s\n",p->gxqm);
	else if(!strcmp(key,"bm"))	
		printf("\033[0;32;24mlinchen#\033[0m%s\n",p->bm);
	else if(!strcmp(key,"dh"))	
		printf("\033[0;32;24mlinchen#\033[0m%s\n",p->dh);
	else if(!strcmp(key,"yx"))	
		printf("\033[0;32;24mlinchen#\033[0m%s\n",p->yx);
	else if(!strcmp(key,"IP"))	
		printf("\033[0;32;24mlinchen#\033[0m%s\n",p->IP);
	else if(!strcmp(key,"Mac"))	
		printf("\033[0;32;24mlinchen#\033[0m%s\n",p->Mac);
	return 0;
}

pack* analysis_recvbuf(int size,char *recvbuf)
{
	pack *r;
	r = (pack *)malloc(sizeof(pack));
	r->size = size;
	r->next = NULL;
	char *p;
	//printf("%s\n",recvbuf);
	p = strchr(recvbuf,':');
	*p = '\0';
	strcpy(r->version,recvbuf);
	//printf("%s\n",r->version);
	recvbuf = p+1;
	p = strchr(recvbuf,':');
	*p = '\0';
	strcpy(r->packnum,recvbuf);
	//printf("%s\n",r->packnum);
	recvbuf = p+1;
	p = strchr(recvbuf,':');
	*p = '\0';
	strcpy(r->username,recvbuf);
	//printf("%s\n",r->username);
	recvbuf = p+1;
	p = strchr(recvbuf,':');
	*p = '\0';
	strcpy(r->hostname,recvbuf);
	//printf("%s\n",r->hostname);
	recvbuf = p+1;
	p = strchr(recvbuf,':');
	*p = '\0';
	strcpy(r->cmd,recvbuf);
	//printf("%s\n",r->cmd);
	recvbuf = p+1;
	p = strchr(recvbuf,'\0');
	strcpy(r->name,recvbuf);
	//printf("%s\n",r->name);

	recvbuf = p+1;
	p = strchr(recvbuf,'\0');
	strcpy(r->bm,recvbuf);
	//printf("%s\n",r->bm);
	recvbuf = p+1;
	p = strchr(recvbuf,'\0');
	strcpy(r->Mac,recvbuf);
	//printf("%s\n",r->Mac);
	recvbuf = p+2;
	p = strchr(recvbuf,'\0');
	strcpy(r->dh,recvbuf);
	//printf("%s\n",r->dh);
	recvbuf = p+1;
	p = strchr(recvbuf,'\0');
	strcpy(r->yx,recvbuf);
	//printf("%s\n",r->yx);
	recvbuf = p+4;
	p = strchr(recvbuf,'\0');
	strcpy(r->IP,recvbuf);
	//printf("%s\n",r->IP);
	recvbuf = p+2;
	p = strchr(recvbuf,'\0');
	strcpy(r->tx,recvbuf);
	//printf("%s\n",r->tx);
	recvbuf = p+10;
	p = strchr(recvbuf,'\0');
	strcpy(r->gxqm,recvbuf);
	//printf("%s\n",r->gxqm);
	recvbuf = p+1;
	p = strchr(recvbuf,'\0');
	strcpy(r->hostnum,recvbuf);
	//printf("%s\n",r->hostnum);

	return r;

}